package cn.wolfcode.car.business.service.impl;

import cn.wolfcode.car.business.domain.Appointment;
import cn.wolfcode.car.business.domain.Statement;
import cn.wolfcode.car.business.mapper.AppointmentMapper;
import cn.wolfcode.car.business.query.AppointmentQuery;
import cn.wolfcode.car.business.service.IAppointmentService;
import cn.wolfcode.car.business.service.IStatementService;
import cn.wolfcode.car.common.base.page.TablePageInfo;
import cn.wolfcode.car.common.exception.BusinessException;
import cn.wolfcode.car.common.util.Convert;
import com.github.pagehelper.PageHelper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;

@Service
@Transactional
public class AppointmentServiceImpl implements IAppointmentService {

    @Autowired
    private AppointmentMapper appointmentMapper;
    @Autowired
    private IStatementService statementService;

    @Override
    public TablePageInfo<Appointment> query(AppointmentQuery qo) {
        PageHelper.startPage(qo.getPageNum(), qo.getPageSize());
        return new TablePageInfo<Appointment>(appointmentMapper.selectForList(qo));
    }

    @Override
    public void save(Appointment appointment) {
        appointmentMapper.insert(appointment);
    }

    @Override
    public Appointment get(Long id) {
        return appointmentMapper.selectByPrimaryKey(id);
    }


    @Override
    public void update(Appointment appointment) {
        Appointment oldObj = this.get(appointment.getId());
        // 判断是否是预约状态,不是则不能编辑
        if (!Appointment.STATUS_APPOINTMENT.equals(oldObj.getStatus())) {
            throw new BusinessException("只有预约状态才可以编辑..");
        }
        appointmentMapper.updateByPrimaryKey(appointment);
    }

    @Override
    public List<Appointment> list() {
        return appointmentMapper.selectAll();
    }

    @Override
    public void deleteBatch(String ids) {
        Long[] dictIds = Convert.toLongArray(ids);
        for (Long dictId : dictIds) {
            Appointment oldObj = this.get(dictId);
            // 判断是否是预约状态,不是则不能取消
            if (!Appointment.STATUS_APPOINTMENT.equals(oldObj.getStatus())) {
                throw new BusinessException("只有预约状态才可以取消..");
            }
            appointmentMapper.deleteByPrimaryKey(dictId,Appointment.DELETE_YES);
        }
    }

    @Override
    public void changStatus(Long id) {
        Appointment oldObj = this.get(id);
        // 判断是否是预约状态,不是则不能进行到店操作
        if (!Appointment.STATUS_APPOINTMENT.equals(oldObj.getStatus())) {
            throw new BusinessException("只有预约状态才可以点击到店操作..");
        }
        appointmentMapper.cancel(id,Appointment.STATUS_ARRIVAL,new Date());
    }

    @Override
    public Long generateStatement(Long appointmentId) {
        Statement statement = statementService.getByAppointmentId(appointmentId);
        if (statement == null) {
            Appointment appointment = appointmentMapper.selectByPrimaryKey(appointmentId);
            statement = new Statement();
            statement.setCreateTime(new Date());
            statement.setActualArrivalTime(appointment.getActualArrivalTime());
            statement.setCarSeries(appointment.getCarSeries()); // 汽车类型
            statement.setCustomerName(appointment.getCustomerName());//客户名称
            statement.setLicensePlate(appointment.getLicensePlate());//车牌号码
            statement.setAppointmentId(appointment.getId());//预约单ID
            statement.setServiceType(appointment.getServiceType().longValue());//服务类型
            statement.setCustomerPhone(appointment.getCustomerPhone());//用户联系方式
            statementService.save(statement);
            appointmentMapper.changeStatus(appointmentId,Appointment.STATUS_SETTLE);//修改预约单状态
        }
        return statement.getId();
    }
}
